package com.ITArmy.service.impl;

import com.ITArmy.beans.*;
import com.ITArmy.beans.Shows.*;
import com.ITArmy.dao.*;
import com.ITArmy.service.MessageService;
import com.ITArmy.util.GetUserSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 *
 */
@Service
public class MessageServiceImpl implements MessageService {
    @Autowired
    MessageMapper messageMapper;
    @Autowired
    FocusMapper focusMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    PraiseMapper praiseMapper;
    @Autowired
    BlogMapper blogMapper;
    @Autowired
    CommentMapper commentMapper;
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    SerializeMapper serializeMapper;

    GetUserSession getUserSession = new GetUserSession();

    @Override
    //用count获取所有的信息具体的数量
    public MassageNumber getUnshowMessageNumber(HttpServletRequest request) {
        long uId = getUserSession.getUser(request).getuId();
        MassageNumber massageNumber = new MassageNumber();
        massageNumber.setCommentNumber(messageMapper.commentNumber(uId));
        massageNumber.setPraiseNumber(messageMapper.praiseNumber(uId));
        massageNumber.setFocusNumber(messageMapper.focusNumber(uId));
        massageNumber.setSystemNumber(messageMapper.systemNumber(uId));
        int a = massageNumber.getCommentNumber() + massageNumber.getFocusNumber() + massageNumber.getPraiseNumber() + massageNumber.getSystemNumber();
        massageNumber.setAll(a);
        return massageNumber;
    }


    /**
     * 获取点赞信息
     */
    @Override
    public List<PraiseMessage> getPrise(HttpServletRequest request) {
        //获取到所有的点赞的id
        List<Message> messages = messageMapper.selectPraiseMessage(getUserSession.getUser(request).getuId());
        //创建一个集合储存所有Praise对象
        System.out.println("一共点赞的条数：" + messages.size());
        List<Praise> praises = new ArrayList<>();
        //创建点赞的信息的集合
        List<PraiseMessage> praiseMessages = new ArrayList<PraiseMessage>();
        if (messages == null) {
            return praiseMessages;
        }
        for (int i = 0; i < messages.size(); i++) {
            //获取到所有的点赞Praise对象
            praises.add(praiseMapper.selectByPrimaryKey(messages.get(i).getpId()));
        }
        System.out.println(praises.size() + "============================");
        for (int i = 0; i < praises.size(); i++) {
            //获取所有点赞博客的信息
            PraiseMessage praiseMessage = new PraiseMessage();
            if (praises.get(i).getbId() != null) {
                //博客信息
                praiseMessage.setBlog(blogMapper.selectByPrimaryKey(praises.get(i).getbId()));
                //点赞信息
                praiseMessage.setPraise(praises.get(i));
                //操作者信息
                praiseMessage.setUser(userMapper.selectByPrimaryKey(praises.get(i).getuId()));
                //将封装的点赞信息储存到集合中
                praiseMessages.add(praiseMessage);
            }
            //获取所有点赞评论的信息
            if (praises.get(i).getcId() != null) {
                //获取点赞的评论
                praiseMessage.setComment(commentMapper.selectByPrimaryKey(praises.get(i).getcId()));
                //获取点赞的信息
                praiseMessage.setPraise(praises.get(i));
                //获取操作者信息
                praiseMessage.setUser(userMapper.selectByPrimaryKey(praises.get(i).getuCid()));
                //将封装的点赞信息储存到集合中
                praiseMessages.add(praiseMessage);
            }

        }
        return praiseMessages;

    }

    /**
     * 关注信息
     */
    @Override
    public List<FocusMessage> getFocus(HttpServletRequest request) {
        List<Message> messageList = messageMapper.selectFocusMessage(getUserSession.getUser(request).getuId());
        List<FocusMessage> focusMessages = new ArrayList<>();
        System.out.println("关注信息的数量为：" + messageList.size());
        for (int i = 0; i < messageList.size(); i++) {
            //动作发生者id
            User user = userMapper.selectByPrimaryKey(messageList.get(i).getuId());
            focusMessages.add(new FocusMessage(user, messageList.get(i)));
        }
        return focusMessages;
    }

    /**
     * 获取评论信息
     */
    @Override
    public List<CommentMessage> getComment(HttpServletRequest request) {
        List<Message> messages = messageMapper.selectCommentMessage(getUserSession.getUser(request).getuId());
        List<CommentMessage> commentMessages = new ArrayList<>();
        if (messages == null) {
            return commentMessages;
        }
        //评论的是博客
        System.out.println("获取的评论信息的大小" + messages.size());
        for (int i = 0; i < messages.size(); i++) {

            CommentMessage commentMessage = new CommentMessage();
            //评论的具体信息
            commentMessage.setComment(commentMapper.selectByPrimaryKey(messages.get(i).getcId()));
            //评论人的信息
            commentMessage.setUser(userMapper.selectByPrimaryKey(messages.get(i).getuId()));
            //被评论博客的信息
            commentMessage.setBlog(blogMapper.selectByPrimaryKey(commentMessage.getComment().getbId()));
            //将封装的评论的信息储存到集合中
            commentMessages.add(commentMessage);
        }
        return commentMessages;
    }

    /**
     * 获取系统信息（订单信息）
     */
    @Override
    public List<SystemMessage> getSystem(HttpServletRequest request) {
        List<Message> messages = messageMapper.selectOrderMessage(getUserSession.getUser(request).getuId());
        List<SystemMessage> systemMessages = new ArrayList<>();
        if (messages == null) {
            return systemMessages;
        }
        for (int i = 0; i < messages.size(); i++) {
            SystemMessage systemMessage = new SystemMessage();
            //订单的具体信息
            systemMessage.setOrder(orderMapper.selectByPrimaryKey(messages.get(i).getoId()));
            //如果购买连载，存入连载具体信息
            if (systemMessage.getOrder().getsId() != null) {
                systemMessage.setSerialize(serializeMapper.selectByPrimaryKey(systemMessage.getOrder().getoId()));
            }
        }
        return systemMessages;
    }

    /**
     * 将所有关注信息设为已读
     */
    @Override
    public int readFocus(long ucid) {
        return messageMapper.readFocus(ucid);
    }

    /**
     * 将所有点赞信息设为已读
     */
    @Override
    public int readPraise(long ucid) {
        return messageMapper.readPraise(ucid);
    }

    /**
     * 将所有点赞信息设为已读
     */
    @Override
    public int readComment(long ucid) {
        return messageMapper.readComment(ucid);
    }

    /**
     * 将所有系统信息设为已读
     */
    @Override
    public int readSystem(long ucid) {
        return messageMapper.readSystem(ucid);
    }

    @Override
    public int deleteFocusMessage(long fid) {
        return messageMapper.deleteFocusMessage(fid);
    }

    @Override
    public int deleteOrderMessage(long oid) {
        return messageMapper.deleteOrderMessage(oid);
    }

    @Override
    public int deletePraiseMessage(long pid) {
        return messageMapper.deleteMessageByPid(pid);
    }

    @Override
    public int deleteCommentMessage(long cid) {
        return messageMapper.deleteMessageByCid(cid);
    }
}
